package com.bank.service.impl;

import java.util.Map;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.DemandAccountMapper;
import com.bank.entity.DemandAccount;
import com.bank.entity.Subject;
import com.bank.exception.CommonException;
import com.bank.service.DemandAccountService;
import com.bank.util.CommonUtil;

@Service(value="demandAccoutService")
public class DemandAccoutServiceImpl implements DemandAccountService {

	Logger logger = Logger.getLogger(DemandAccoutServiceImpl.class);
	
	@Autowired
	DemandAccountMapper  demandAccountMapper;
	
	/**
	 * 
	 * 活期开户
	 * @param  账户信息
	 * 
	 */
	@Transactional
	public void openAccount(Map<String, Object> params) throws CommonException {
		// TODO Auto-generated method stub
		DemandAccount demandAccount = null;
		Subject subject = CommonUtil.getSubjectInfo(CommonUtil.DEMAND_ACCT_SUBJECT);
		if(subject == null){
			throw new CommonException(RespCode.DATA_IS_NULL, "科目号未配置");
		}
		String  number = (String)params.get("accounting_ori")+(String)params.get("senior_oir")+subject.getSubject_number()+(String)params.get("currency")+"01";
		demandAccount = demandAccountMapper.selectByPrimaryKey(number);
		if(demandAccount!= null){
			throw new CommonException(RespCode.ACCT_EXISTS, "账号已存在");
		}else
		{
			demandAccount = new DemandAccount();
		}
		demandAccount.setNumber(number);//账号
		demandAccount.setAccounting_ori((String)params.get("accounting_ori"));
		demandAccount.setSenior_oir((String)params.get("senior_oir"));
		demandAccount.setAccount_subject(CommonUtil.DEMAND_ACCT_SUBJECT);
		demandAccount.setBalance_direct(subject.getSubject_balance_direct());
		demandAccount.setBalance(0.00);
		demandAccount.setBalance_pre(0.00);
		demandAccount.setCurrency((String)params.get("currency"));
		demandAccount.setCustomer_number((String)params.get("customer_number"));
		demandAccount.setCustomer_name((String)params.get("customer_name"));
		demandAccount.setLast_trade_date("0");
		demandAccount.setProduct_number(0.00);
		demandAccount.setRate_id((String)params.get("rate_value"));
		demandAccount.setPassword(CommonUtil.passwordEncryption((String)params.get("password")));
		demandAccount.setAccount_status("0");
		demandAccountMapper.insertSelective(demandAccount);
	}

	
	
}
